package com.app.medicalcontrol.fragment.Robot.child.item;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.medicalcontrol.R;
import com.app.medicalcontrol.basis.BasisFragment;
import com.app.medicalcontrol.basis.Constants;
import com.app.medicalcontrol.bean.Acupoint;
import com.app.medicalcontrol.bean.AcupointListBean;
import com.app.medicalcontrol.bean.BasisBean;
import com.app.medicalcontrol.bean.LoginBean;
import com.app.medicalcontrol.bean.Robot;
import com.app.medicalcontrol.fragment.Robot.child.RobotItemFragment;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.OptionPicker;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

public class IJSetFragment extends BasisFragment {

    private OptionPicker picker;
    @BindView(R.id.tv_left_acupoint_name)
    TextView mTvLeftAcupName;
    @BindView(R.id.tv_right_acupoint_name)
    TextView mTvRightAcupName;
    @BindView(R.id.et_time_left)
    EditText mEtTimeLeft;
    @BindView(R.id.et_time_right)
    EditText mEtTimeRight;
    @BindView(R.id.et_temperature_left)
    EditText mEtTemperLeft;
    @BindView(R.id.et_temperature_right)
    EditText mEtTemperRight;
    @BindView(R.id.edit_name_left)
    EditText mEtNameLeft;
    @BindView(R.id.edit_name_right)
    EditText mEtNameRight;

    private int leftAcupointId;
    private int rightAcupointId;

    private Robot bean;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_ij_set);
        setTitle(""+getString(R.string.robot_tab_second));
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        getList();
    }

    private void getList() {
        showProgress();
        RequestParams params = new RequestParams();
        params.put("robot_id",RobotItemFragment.robotId);
        HttpRestClient.get(Constants.URL_ROBOT_INFO, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                            bean = (Robot) result;
                    }
                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, Robot.class);
    }

    @OnClick(R.id.btn_choose_left_acupoint)
    public void getLeftAcupoint(){
        showProgress();
        RequestParams params = new RequestParams();
        HttpRestClient.post(Constants.URL_ACUPOINT_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        final AcupointListBean listBean = (AcupointListBean) result;
                        List<String> names = new ArrayList<>();
                        for (Acupoint bean:listBean.getList()){
                            names.add(bean.getName());
                        }

                        picker= new OptionPicker((Activity) mContext, names);
                        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                            @Override
                            public void onOptionPicked(int index, String item) {
                                mTvLeftAcupName.setText(item);
                                leftAcupointId = listBean.getList().get(index).getAcupoint_id();
                            }
                        });
                        picker.show();
                        dismissProgress();
                    }

                    @Override
                    public void onFinish(int httpWhat) {

                    }
                },
                0, AcupointListBean.class);
    }

    @OnClick(R.id.btn_choose_right_acupoint)
    public void getRightAcupoint(){
        showProgress();
        RequestParams params = new RequestParams();
        HttpRestClient.post(Constants.URL_ACUPOINT_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        final AcupointListBean listBean = (AcupointListBean) result;
                        final List<String> names = new ArrayList<>();
                        for (Acupoint bean:listBean.getList()){
                            names.add(bean.getName());
                        }
                        picker= new OptionPicker((Activity) mContext, names);
                        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                            @Override
                            public void onOptionPicked(int index, String item) {
                                mTvRightAcupName.setText(item);
                                rightAcupointId = listBean.getList().get(index).getAcupoint_id();
                            }
                        });
                        picker.show();
                        dismissProgress();
                    }

                    @Override
                    public void onFinish(int httpWhat) {

                    }
                },
                0, AcupointListBean.class);
    }

    @OnClick(R.id.btn_start)
    public void start(){

        String leftAcupName= mTvLeftAcupName.getText().toString().trim();
        String rightAcupName= mTvRightAcupName.getText().toString().trim();
        if(leftAcupName.equals("请选择穴位") && rightAcupName.equals("请选择穴位")) {
            showToast("请选择穴位");
            return;
        }

        String leftTime= mEtTimeLeft.getText().toString().trim();
        if(TextUtils.isEmpty(leftTime)){
            showToast("左手时长不能为空");
            return;
        }

        String rightTime= mEtTimeRight.getText().toString().trim();
        if(TextUtils.isEmpty(rightTime)){
            showToast("右手时长不能为空");
            return;
        }

        String leftTemper= mEtTemperLeft.getText().toString().trim();
        if(TextUtils.isEmpty(leftTemper)){
            showToast("左手温度不能为空");
            return;
        }

        String rightTemper= mEtTemperRight.getText().toString().trim();
        if(TextUtils.isEmpty(rightTemper)){
            showToast("右手温度不能为空");
            return;
        }


        String leftIJName= mEtNameLeft.getText().toString().trim();
        if(TextUtils.isEmpty(leftIJName)) {
            showToast("请输入左手艾灸名称");
            return;
        }

        String rightIJName= mEtNameRight.getText().toString().trim();
        if( TextUtils.isEmpty(rightIJName)) {
            showToast("请输入右手艾灸名称");
            return;
        }


        showProgress();
        RequestParams params = new RequestParams();
        params.put("left_hand_id", bean.getLeft_hand_id());
        params.put("left_acupoint_id", leftAcupointId);
        params.put("left_moxibustion_time", mEtTimeLeft.getText().toString());
        params.put("left_moxibustion_temperature", mEtTemperLeft.getText().toString());
        params.put("right_hand_id",bean.getRight_hand_id());
        params.put("right_acupoint_id", rightAcupointId);
        params.put("right_moxibustion_time", mEtTimeRight.getText().toString());
        params.put("right_moxibustion_temperature", mEtTemperRight.getText().toString());
        params.put("right_moxibustion_temperature", mEtTemperRight.getText().toString());
        params.put("robot_id",RobotItemFragment.robotId);

        HttpRestClient.post(Constants.URL_SET_HAND, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        BasisBean listBean = (BasisBean) result;
                        showToast("开始艾灸");

                        mEtTimeLeft.setText(null);
                        mEtTimeRight.setText(null);

                        mEtTemperLeft.setText(null);
                        mEtTemperRight.setText(null);

                        mTvLeftAcupName.setText("请选择穴位");
                        mTvRightAcupName.setText("请选择穴位");

                        mEtNameLeft.setText(null);
                        mEtNameRight.setText(null);

                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, BasisBean.class);
    }

}
